﻿namespace RuoTeng.Util
{
    using System.Collections.Generic;

   public abstract class Task
    {  
        /// <summary>
        /// 任务状态标志
        /// </summary>
        public enum TaskState
        {
          /// <summary> 运行</summary>
            Start = 0x01,        
          /// <summary> 暂停</summary>
            Pause = 0x02,          
          /// <summary> 结束</summary>
            Stop  = 0x03
        }

        /// <summary>
        /// 任务状态改变的事件函数
        /// </summary>
        /// <param name="work">任务对象</param>
        /// <param name="workState">任务状态</param>
        public delegate void TaskEventHandler(Task work);

        #region  任务事件句柄
        /// <summary>
        /// 事件订阅对象
        /// </summary>
        public event TaskEventHandler ShowStart;
        /// <summary>
        /// 事件订阅对象
        /// </summary>
        public event TaskEventHandler ShowPause;
        /// <summary>
        /// 事件订阅对象
        /// </summary>
        public event TaskEventHandler ShowStop;
        /// <summary>
        /// 事件订阅对象
        /// </summary>

        #endregion;

        /// <summary> 当前任务的状态</summary>
        private TaskState _State = TaskState.Stop;
        public TaskState State
        {
            get { return this._State; }
            private set { _State = value; }
        }

        /// <summary> 需要切换的状态</summary>
        private   TaskState _ToChangeState = TaskState.Start;
        protected TaskState ToChangeState
        {
            get { return this._ToChangeState; }
            private set { this._ToChangeState = value; }
        }

        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
           //任务暂停中,由容器来调用 【停止】函数 
            if (this.State == TaskState.Stop || this.State == TaskState.Pause)
            {
                this.State = this.ToChangeState = TaskState.Start;
                this.State = this.ToChangeState;
                if (this.State == TaskState.Start)
                {
                    this.OnStart();
                }
            }            
        }
        protected virtual  void OnStart()
        {
            this.State = TaskState.Start;    
            if (this.ShowStart != null)
            {
               this.ShowStart(this);
            }
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            //执行暂停操作      
            if (this.State == TaskState.Start)
            {
               this.ToChangeState = TaskState.Pause;
            }
        }
        protected virtual void OnPause()
        {
            this.State = TaskState.Pause;
            if (this.ShowPause != null)
            {
                this.ShowPause(this);
            }
        }

        /// <summary>
        /// 停止 是否中强制停止还是任务完成停止 由调用者 提供
        /// </summary>
        public void Stop()
        {          
         //任务暂停中,由容器来调用 【停止】函数 
            if (this.State == TaskState.Pause)
            {             
               this.State = this.ToChangeState = TaskState.Stop;
               this.OnStop();
            }
          //任务正在运行,由【业务逻辑】来调用 【停止】函数 
            else if (this.State == TaskState.Start)
            {
                this.ToChangeState = TaskState.Stop;
            }
        }

        protected virtual void OnStop()
        {
            this.State = TaskState.Stop;
            if (this.ShowStop != null)
            {
                this.ShowStop(this);
            }
        }

        /// <summary>
        /// 检测是否需要打断任务
        /// </summary>
        protected bool IsRequireBreak
        {
            get
            {
                return  (this.ToChangeState == TaskState.Pause||this.ToChangeState==TaskState.Stop)? true : false;
            }
        }       
    }
}
